package ncorps.commun;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import ncorps.fourier.NCorps_Fourier;
import ncorps.gui.ParametersWindow;
import ncorps.newton.NCorps_Newton;
import ncorps.output.ConsoleFrame;
import ncorps.output.FileOut;
import ncorps.output.ImageOut;

public class CalculThread extends Thread {

    int temps;
    double dt;
    int tempsMax;
    int _N;
    int _R;
    ConditionInitiale CI;
    ArrayList<ConditionInitiale> LCIn, LCIf;
    String _RepImages, _RepFiles, _RepStat;
    String imagenametype;
    String filenametype;
    String format;
    ParametersWindow console;
    boolean calculparfourier, fourierimagedemande, fourierfichierdemande, calculparnewton, newtonimagedemande, newtonfichierdemande;

    public CalculThread(ParametersWindow console, int temps, double dt, int tempsMax, int _N, int _R, String _RepImages, String _RepFiles, String _RepStat, String imagenametype, String filenametype, String format, boolean calculparfourier, boolean fourierimagedemande, boolean FficOK, boolean newtonOK, boolean NimgOK, boolean NFicOK) {
        this.calculparfourier = calculparfourier;
        this.fourierimagedemande = fourierimagedemande;
        this.fourierfichierdemande = FficOK;
        this.calculparnewton = newtonOK;
        this.newtonimagedemande = NimgOK;
        this.newtonfichierdemande = NFicOK;
        //
        this.console = console;
        this.temps = temps;
        this.dt = dt;
        this.tempsMax = tempsMax;
        this._N = _N;
        this._R = _R;
        this._RepImages = _RepImages;
        this._RepFiles = _RepFiles;
        this._RepStat = _RepStat;
        this.imagenametype = imagenametype;
        this.filenametype = filenametype;
        this.format = format;
        LCIn = new ArrayList<ConditionInitiale>(tempsMax);
        LCIf = new ArrayList<ConditionInitiale>(tempsMax);
        CI = new ConditionInitiale(_R, _N);
        LCIn.add(CI);
        LCIf.add(CI);
    }

    public void calcul_loop() throws IOException {
        JTextField timefield = console.getTimefield();
        if (this.calculparfourier) {
            while (temps < tempsMax - 1) {
                NCorps_Fourier fourier = new NCorps_Fourier(LCIf.get(temps), dt);
                LCIf.add(temps + 1, new ConditionInitiale(CI, fourier.getGcoord()));
                if (this.fourierfichierdemande) {
                    FileOut.printf(_RepFiles, filenametype + "F" + temps + ".txt", fourier.getGcoord(), _N, false);
                }
                if (this.fourierimagedemande) {
                    new ImageOut().printi1(_RepImages, imagenametype + "F" + temps, format, fourier.getGcoord(), LCIf.get(temps));
                }

                timefield.setText("Fourier : " + String.valueOf(temps) + "/" + tempsMax);
                temps++;
            }
        }
        if (this.calculparnewton) {
            temps = 0;
            while (temps < tempsMax - 1) {
                NCorps_Newton newton = new NCorps_Newton(LCIn.get(temps), dt);
                LCIn.add(temps + 1, new ConditionInitiale(CI, newton.getGcoord()));
                if (this.newtonfichierdemande) {
                    FileOut.printf(_RepFiles, filenametype + "N" + temps + ".txt", newton.getGcoord(), _N, false);
                }
                if (this.newtonimagedemande) {
                    new ImageOut().printi1(_RepImages, imagenametype + "N" + temps, format, newton.getGcoord(), LCIn.get(temps));
                }
                timefield.setText("Newton : " + String.valueOf(temps) + "/" + tempsMax);
                temps++;
            }
        }
    }

    @Override
    public void run() {
        try {
            calcul_loop();
        } catch (IOException ex) {
            Logger.getLogger(CalculThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}









